package com.junqson.je.msg.controller;

import com.junqson.je.common.UserHolder;
import com.junqson.je.common.annotation.LoginRequire;
import com.junqson.je.common.model.ApiResult;
import com.junqson.je.msg.model.MessageModel;
import com.junqson.je.msg.service.MsgCrudService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Descriptions 用户消息中心
 * @Company
 * @Author Junqson
 * @Date 2020/2/10 15:07
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("message")
public class MessageController {

    @Autowired
    MsgCrudService msgCrudService;

    /**
     * 存储站内信
     **/
    @RequestMapping("save")
    public ApiResult saveMessage(
            @RequestBody MessageModel message) {
        log.info("message: " + message);
//        int userId = UserHolder.get().getUserId();

//        if (message.getFromId() != null) {
//            if (message.getFromId() != userId) {
//                return ApiResult.error("无权操作");
//            }
//        }
        if (StringUtils.isEmpty(message.getContent())) {
            return ApiResult.error("内容不能为空");
        }

        if (message.getToId() == null) {
            return ApiResult.error("to id is null");
        }
        message.genConversationId();
        return ApiResult.success(msgCrudService.save(message));
    }

    /**
     * @return Message 站内信
     * @method getMessage 获取一个用户接收的站内信
     **/
    @LoginRequire
    @RequestMapping("/user/get")
    public ApiResult getMessage() {
        int userId = UserHolder.get().getUserId();
        return ApiResult.success(msgCrudService.findByToId(userId));
    }

    /**
     * 读站内信
     **/
    @LoginRequire
    @RequestMapping("/read")
    public ApiResult readMessage(Long msgId) {
        if (msgId == null) {
            return ApiResult.error("msg id is null");
        }
        int userId = UserHolder.get().getUserId();
        msgCrudService.read(msgId, userId);
        return ApiResult.success();
    }

    /**
     * @return Message 站内信
     * @method getSendMessage 获取一个用户发送的站内信
     **/
    @LoginRequire
    @RequestMapping("/user/send")
    public ApiResult getSendMessage() {
        int userId = UserHolder.get().getUserId();
        return ApiResult.success(msgCrudService.findByFromId(userId));
    }

    /**
     * ws推送未读数目
     * @return
     */
    @RequestMapping("/sendCount")
    @LoginRequire
    public ApiResult sendCount() {
        int userId = UserHolder.get().getUserId();
        msgCrudService.sendCount(userId);
        return ApiResult.success();
    }

    /**
     * 获取未读数目
     * @return
     */
    @LoginRequire
    @GetMapping("/count/not")
    public ApiResult getNotReadCount() {
        int userId = UserHolder.get().getUserId();
        return ApiResult.success(msgCrudService.findNotRead(userId).size());
    }


}
